<?php
class Admin_DepartmentsController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pDid = empty($p['d_id'])?'':Tool_Fnc::safe_string($p['d_id']);
        $pIntroduce = empty($p['introduce'])?'':Tool_Fnc::safe_string($p['introduce']);
        $pCreated = empty($p['created'])?'':Tool_Fnc::safe_string($p['created']);

        $tWhere = ' 1 ';

        if(!empty($pIntroduce)){
            $tWhere .= ' and introduce like \'%'.$pIntroduce.'%\'';
        }

        if(!empty($pCreated)){
            $tWhere .= ' and FROM_UNIXTIME(created,\'%Y-%m-%d\') = \''.$pCreated.'\'';
        }

        $tDIMO = new DepartmentinfoModel;

        $tCount = $tDIMO->field('count(*) c')->where($tWhere)->fList();
        $tCnt = empty($tCount[0]['c'])?0:Tool_Fnc::safe_string($tCount[0]['c']);
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        if(!empty($tCnt)){
            $tDatas = $tDIMO->field('*')->where($tWhere)->order('id desc')->limit($tPage->limit())->fList();
        }

        if(!empty($pDid)){//编辑页面显示
            $tMLData = $tDIMO->field('*')->where('id = '.$pDid)->fRow();
            Tool_Fnc::ajaxMsg('',1,$tMLData);
        }

        $this->assign('pIntroduce',$pIntroduce);
        $this->assign('pCreated',$pCreated);
        $this->assign('tDatas',$tDatas);
        $this->assign('tShow',$tPage->show());
    
    }
    public function addAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pIntroduce = empty($p['introduce'])?Tool_Fnc::ajaxMsg('部门名称不能为空'):Tool_Fnc::safe_string($p['introduce']);
            $pNum = empty($p['num'])?Tool_Fnc::ajaxMsg('部门编码不能为空'):Tool_Fnc::safe_string($p['num']);
            $pAddress = empty($p['address'])?Tool_Fnc::ajaxMsg('地址不能为空'):Tool_Fnc::safe_string($p['address']);
            $pPhone = empty($p['phone'])?Tool_Fnc::ajaxMsg('电话不能为空'):Tool_Fnc::safe_string($p['phone']);

            $tTime = time();

            $tMO = new DepartmentinfoModel;
            $tMData = $tMO->field('id')->where('introduce = \''.$pIntroduce.'\' and num = '.$pNum)->fRow();
            if(!empty($tMData['id'])){
                Tool_Fnc::ajaxMsg('该部门已经添加过了,请重新填写其他名称和编码');
            }
            $tData = array(
                'num' => $pNum,
                'introduce' => $pIntroduce,
                'address' => $pAddress,
                'phone' => $pPhone,
                'created' => $tTime,
            );
            if(!$tId = $tMO->insert($tData)){
                Tool_Fnc::ajaxMsg('添加失败');
            }
            Tool_Fnc::ajaxMsg('添加成功',1);
        }
    
    }
    public function editAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pDid = empty($p['d_id'])?Tool_Fnc::ajaxMsg('ID错误'):Tool_Fnc::safe_string($p['d_id']);
            $pIntroduce = empty($p['introduce'])?Tool_Fnc::ajaxMsg('部门名称不能为空'):Tool_Fnc::safe_string($p['introduce']);
            $pNum = empty($p['num'])?Tool_Fnc::ajaxMsg('部门编码不能为空'):Tool_Fnc::safe_string($p['num']);
            $pAddress = empty($p['address'])?Tool_Fnc::ajaxMsg('地址不能为空'):Tool_Fnc::safe_string($p['address']);
            $pPhone = empty($p['phone'])?Tool_Fnc::ajaxMsg('电话不能为空'):Tool_Fnc::safe_string($p['phone']);

            $tTime = time();

            $tMO = new DepartmentinfoModel;
            $tMData = $tMO->field('id')->where('introduce = \''.$pIntroduce.'\' and num = '.$pNum)->fRow();
            $tData = array(
                'num' => $pNum,
                'introduce' => $pIntroduce,
                'address' => $pAddress,
                'phone' => $pPhone,
                'updated' => $tTime,
                'id' => $pDid,
            );
            if(!$tMO->update($tData)){
                Tool_Fnc::ajaxMsg('修改失败');
            }
            Tool_Fnc::ajaxMsg('更新成功',1);
        }
    
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['id']);

        $tMO = new DepartmentinfoModel;
        if(!$tMO->del($pId)){
            Tool_Fnc::ajaxMsg('删除失败');
        }
        Tool_Fnc::ajaxMsg('删除成功',1);
    
    }
}
?>
